package com.yuoj.yuojcodesandbox.sandbox.docker;


import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.command.LogContainerResultCallback;

import java.util.List;

/**
 * @author 刘渠好
 * @since 2025/3/28 17:10
 * java操作docker常用操作
 */
public class DockerDemo {
    public static void main(String[] args) throws InterruptedException {
        //获取默认docker客户端
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();

        //1.拉取镜像
        String imageName = "nginx:latest";
        PullImageCmd pullImageCmd = dockerClient.pullImageCmd(imageName);

        //拉取镜像回滚操作
        PullImageResultCallback pullImageResultCallback = new PullImageResultCallback() {
            @Override
            public void onNext(PullResponseItem item) {
                System.out.println("Pull Image: " + item.toString());
                super.onNext(item);
            }
        };
        pullImageCmd.exec(pullImageResultCallback)
                //等待执行完成，才会继续往下执行
                .awaitCompletion();
        System.out.println("拉取成功！");

        //2.创建容器
        CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(imageName);
        CreateContainerResponse containerResponse = createContainerCmd.exec();
        System.out.println(containerResponse);

        //3.查看容器状态
        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
        List<Container> containers = listContainersCmd.
                //显示所有容器信息
                        withShowAll(true).
                exec();
        for (Container container : containers) {
            System.out.println(container);
        }

        //4.启动容器
        String containerId = containerResponse.getId();
        dockerClient.startContainerCmd(containerId).exec();

        //5.查看日志
        LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId);
        LogContainerResultCallback containerResultCallback = new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                //日志类型
                System.out.println("Stream Type:" + item.getStreamType());
                //输出日志信息
                System.out.println("日志:"+new String(item.getPayload()));
                super.onNext(item);
            }
        };

        //阻塞等待日志输出
        logContainerCmd
                .withStdOut(true)
                .withStdErr(true)
                .exec(containerResultCallback)
                .awaitCompletion();

        //6.删除容器
        dockerClient.removeContainerCmd(containerId)
                //强制删除容器
                .withForce(true)
                .exec();

        //7.删除镜像
        dockerClient.removeImageCmd(imageName)
                .withForce(true)
                .exec();
    }


}
